package com.nosweattechnet.datatypes;

import org.json.JSONException;
import org.json.JSONObject;

public class Item implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	
	//Characteristics of an Item
	private String uuid;
	private String name;
	private String description;
	private String placeid;
	private float xpos;
	private float ypos;
	private float zpos;
	private String userid;
	
	//Names of Columns in the database corresponding to each member variable
	private static final String itemIDString="item_id";
	private static final String itemNameString="item_name";
	private static final String itemDescripString="item_descrip";
	private static final String placeIDString="place_id";
	private static final String locxString="loc_x";
	private static final String locyString="loc_y";
	private static final String loczString="loc_z";
	private static final String userIDString="user_id";
	
	//Default values if the corresponding columns are not supplied from database
	private static final String defaultitemIDValue="";
	private static final String defaultitemNameValue="";
	private static final String defaultitemDescripValue="";
	private static final String defaultplaceIDValue="";
	private static final float defaultlocxValue=0;
	private static final float defaultlocyValue=0;
	private static final float defaultloczValue=0;
	private static final String defaultuserIDValue="";
	
	//Makes an item
	public Item(String id, String n,String d,String pid,float xp,float yp,float zp,String uid){
		uuid=id;
		name=n;
		description=d;
		placeid=pid;
		xpos=xp;
		ypos=yp;
		zpos=zp;
		userid=uid;
	}
	
	//Makes an Item based on a jsonobject supplied from the database
	public Item(JSONObject jobj){
		try {
			uuid=jobj.getString(Item.itemIDString);
		} catch (JSONException e) {
			uuid=Item.defaultitemIDValue;
		}
		try {
			name=jobj.getString(Item.itemNameString);
		} catch (JSONException e) {
			name=Item.defaultitemNameValue;
		}
		try {
			description=jobj.getString(Item.itemDescripString);
		} catch (JSONException e) {
			description=Item.defaultitemDescripValue;
		}
		try {
			placeid=jobj.getString(Item.placeIDString);
		} catch (JSONException e) {
			placeid=Item.defaultplaceIDValue;
		}
		try {
			xpos=(float)jobj.getDouble(Item.locxString);
		} catch (JSONException e) {
			xpos=Item.defaultlocxValue;
		}
		try {
			ypos=(float)jobj.getDouble(Item.locyString);
		} catch (JSONException e) {
			ypos=Item.defaultlocyValue;
		}
		try {
			zpos=(float)jobj.getDouble(Item.loczString);
		} catch (JSONException e) {
			zpos=Item.defaultloczValue;
		}
		try {
			userid=jobj.getString(Item.userIDString);
		} catch (JSONException e) {
			userid=Item.defaultuserIDValue;
		}
		
	}
	
	//Getters
	public String getName()
	{
		return name;
	}
	
	public String getDescription()
	{
		return description;
	}
	
	public float getXPos(){
		return xpos;
	}
	
	public float getYPos(){
		return ypos;
	}
	
	public float getZPos(){
		return zpos;
	}

	public String getId() {
		return uuid;
	}
	
	public String getPlaceId(){
		return placeid;
	}
	
	public String getUserId(){
		return userid;
	}
	
	//Assigns an ID to an Item which has none
	//Used when adding new items to the database, because the id is not known till after the request is made
	public void assignID(String id){
		if(uuid==null){
			uuid=id;
		}
	}
	
	//Two items are considered the same if and only if they have the same ID
	public boolean equals(Item i){
		return uuid==i.getId();
	}
	

}
